/**
 * 
 */
package com.sci.web.action.util;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 所有Action的父类，都要集成该类（需要向页面传值的类继承，其他的Action直接继承ActionSupport即可）
 * 
 * @author BaiFuyou
 * @date: Sep 22, 2011 3:54:54 PM
 */
public class BaseAction extends ActionSupport {
	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	public String getJSONObjectString(Object object) {
		return JSONObject.fromObject(object).toString();
	}

	public String getJSONArrayString(Object object) {
		return JSONArray.fromObject(object).toString();
	}

	public void outPrint(String json) {
		PrintWriter out = null;
		try {
			out = this.getResponse().getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.print(json);
		out.close();
	}

	public void setContentType(String ContentType) {
		this.getResponse().setContentType(ContentType);
	}

	public void outXmlString(String jsonstring) {
		this.getResponse().setContentType("application/xml;charset=UTF-8");
		this.outPrint(jsonstring);
	}

	/**
	 * text/html;charset=utf-8
	 */
	public static final String HTML = "text/html;charset=utf-8";
	/**
	 * application/xml;charset=UTF-8
	 */
	public static final String xml = "application/xml;charset=UTF-8";
	/**
	 * application/json;charset=UTF-8
	 */
	public static final String JSON = "application/json;charset=UTF-8";
	/**
	 * 
	 */
	public static final String htmlgb = "text/html;charset=gb2312";
}
